﻿using Biz;
using BusinessLayer.Biz;
using BusinessLayer.Dal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Models;
namespace Web.Controllers
{
    public class CartController : Controller
    {
        //
        // GET: /TestCart/
        //DatabaseContext db = new DatabaseContext();
        private Product productbiz = new Product();
        private Order order;
        private Unsold unsold = new Unsold();
        private string CartSessionKey = "CartSession";

        public ActionResult Index()
        {
            var cart = GetAllFromCart();
            ViewBag.CartCount = cart.GetCount();
            ViewBag.CartTotal = cart.GetTotal();
            ViewBag.CartItems = cart.ListItem;
            ViewBag.message = TempData["message"];
            return View();
        }
        public ActionResult AddToCart(int id)
        {
            Product product = productbiz.GetProductById(id);
            if (!AddToCartSession(product, 1))
            {
                TempData["message"] = "Sản phẩm" + product.Name + " hiện chỉ còn " + unsold.GetQuantityByProduct(product) + " cái";
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");
        }
        public ActionResult RemoveFromCart(int id)
        {
            var cart = GetAllFromCart();
            cart.RemoveFromCart(id);
            return RedirectToAction("Index");
        }

        public ActionResult RemoveCart()
        {
            if (IsCartExistInSession())
            {
                Session[CartSessionKey] = null;
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult UpdateCart(FormCollection collection)
        {
            int id=int.Parse(collection["product"]);
            int quantity;
            if (!int.TryParse(collection["quantity"].ToString(),out quantity))
            {
                return RedirectToAction("Index");
            }
            Product product = productbiz.GetProductById(id);
            if (!UpdateCartSession(product, quantity))
            {
                TempData["message"] = "Sản phẩm" + product.Name + " hiện chỉ còn " + unsold.GetQuantityByProduct(product) + " cái";
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Checkout(FormCollection collection)
        {
            Cart cart = GetAllFromCart();
            if (cart.ListItem.Count() == 0)
            {
                return RedirectToAction("Index");
            }
            order =new Order();
            order.CustomerName = collection["name"];
            order.CustomerEmail = collection["email"];
            order.CustomerPhone = collection["phone"];
            order.CustomerAddress = collection["address"];
            order.Time = DateTime.Now;
            order.Status = OrderStatus.UN_PAID;
            order.Insert(cart);
            RemoveCart();
            return View();
        }
        
        //----------------------------//
        
        public bool IsCartExistInSession()
        {
            return Session[CartSessionKey] as Cart != null;
        }
        bool AddToCartSession(Product product, int quantity)
        {
            Cart cart = GetAllFromCart();
            if (!cart.AddToCart(product, quantity)) 
            {
                return false;
            }
            Session[CartSessionKey] = cart;
            return true;
        }
        public Cart GetAllFromCart()
        {
            Cart cart = null;
            if (IsCartExistInSession())
            {
                cart = Session[CartSessionKey] as Cart;
            }
            else
            {
                cart = new Cart();
            }
            return cart;
        }

        public bool UpdateCartSession(Product product, int quantity)
        {
            if (IsCartExistInSession())
            {
                Cart cart = GetAllFromCart();
                if (!cart.UpdateQuantity(product, quantity))
                {
                    return false;
                }
                Session[CartSessionKey] = cart;
            }
            return true;
        }

        

        //public ActionResult ShowCart()
        //{
        //    var cart = GetCart(this.HttpContext);
        //    ViewBag.CartCount = cart.GetCount();
        //    ViewBag.CartTotal = cart.GetTotal();
        //    ViewBag.Items = cart.ListItem;
        //    return View();
        //}
        
    }
}
